package com.huika.dealontop.control.timelimit.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huika.dealontop.R;
import com.huika.dealontop.control.base.BaseAda;
import com.huika.dealontop.control.base.extend.BaseViewHolder;
import com.huika.dealontop.control.homepage.activity.SearchActivity;
import com.huika.dealontop.support.domain.SearchResultBean;
import com.huika.dealontop.utils.DistanceUtils;
import com.huika.dealontop.utils.GMapUtil;
import com.huika.dealontop.utils.ImageLoaderHelper;
import com.huika.dealontop.utils.UIUtils;
import com.huika.huikalib.utils.MoneyShowTool;
import com.huika.huikalib.utils.StringUtils;

/**
 * 分类附近子节点适配器
 */
public class SeachCommonAdapter extends BaseAda<SearchResultBean> {
    private Context context;
    private int type;

    public SeachCommonAdapter(Context context) {
        super(context);
        this.context = context;
    }


    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        SearchResultBean listbean = getItem(i);
        BaseViewHolder holder;
        if (view == null) {
            //根据type new holder
            if (type == 1) {
                holder = new MerchantItemHolder(mContext);
            } else if (type == 0) {
                holder = new CommentItemHolder(mContext);
            } else {
                holder = new ProductItemHolder(mContext);
            }
            view = holder.getRootView();
        } else {
            holder = (BaseViewHolder) view.getTag();
        }
        holder.setData(listbean);
        return view;
    }


    class MerchantItemHolder extends BaseViewHolder<SearchResultBean> {
        private RelativeLayout business_rl;
        private LinearLayout project_ll;
        private ImageView business_iv_icon;
        private TextView business_tv_name, business_tv_address, business_tv_juli, business_tv_pingfennum;
        private RatingBar business_rb_pingfen;

        public MerchantItemHolder(Context context) {
            super(context);
        }

        @Override
        protected View initView() {
            return View.inflate(UIUtils.getContext(), R.layout.seach_item, null);
        }

        @Override
        protected void assignView(View rootView) {
            //商品隐藏
            project_ll = (LinearLayout) rootView.findViewById(R.id.project_ll_all);
            business_rl = (RelativeLayout) rootView.findViewById(R.id.business_rl);
            business_iv_icon = (ImageView) rootView.findViewById(R.id.business_iv_icon);
            business_tv_name = (TextView) rootView.findViewById(R.id.business_tv_name);
            business_tv_address = (TextView) rootView.findViewById(R.id.business_tv_address);
            business_tv_juli = (TextView) rootView.findViewById(R.id.business_tv_juli);
            business_tv_pingfennum = (TextView) rootView.findViewById(R.id.business_tv_pingfennum);
            business_rb_pingfen = (RatingBar) rootView.findViewById(R.id.business_rb_pingfen);
            project_ll.setVisibility(View.GONE);
        }

        @Override
        protected void refreshView(SearchResultBean data) {
            if (data.getType() == 1) {
                if (StringUtils.isEmpty(data.getCouMerImage())) {
                    business_iv_icon.setImageResource(R.mipmap.morenpic);
                } else {
                    ImageLoaderHelper.displayImage(data.getCouMerImage(), business_iv_icon);
                }
                business_tv_name.setText(data.getCouMerName());
                business_tv_address.setText(data.getMerchantAddress() + "");
                if (new GMapUtil().getCurrentLat().equals("") || StringUtils.isEmpty(new GMapUtil().getCurrentLat())
                        || new
                        GMapUtil().getCurrentLng().equals("") || StringUtils.isEmpty(new GMapUtil().getCurrentLng())
                        || new GMapUtil().getCurrentLng().equals("0.0") || new GMapUtil().getCurrentLat().equals("0" +
                        ".0")) {
                    business_tv_juli.setText(R.string.common_unknown);
                } else {
                    try {
                        if (data.getDistance() > 0) {
                            business_tv_juli.setText(DistanceUtils.formatMeter(data.getDistance()));
                        } else {
                            business_tv_juli.setText(R.string.common_unknown);
                        }
                    } catch (Throwable t) {
                        business_tv_juli.setText(R.string.common_unknown);
                    }
                }
            /*business_tv_juli.setText(data.getDiscount());*/
                if (Float.parseFloat(MoneyShowTool.formatOneStr(data.getEvaluateScore())) > 0) {
                    try {
                        business_rb_pingfen.setRating(Float.parseFloat(data.getEvaluateScore()));
                        business_tv_pingfennum.setText(Float.parseFloat(data.getEvaluateScore()) + "");
                    } catch (Throwable t) {
                        business_rb_pingfen.setRating(5f);
                        business_tv_pingfennum.setText("5.0");
                    }
                } else {
                    business_rb_pingfen.setRating(5f);
                    business_tv_pingfennum.setText("5.0");
                }
            } else {
                business_rl.setVisibility(View.GONE);
            }
        }
    }

    class ProductItemHolder extends BaseViewHolder<SearchResultBean> {
        private ImageView iv_picture;
        private TextView tv_name, tv_price, tv_zhekounum, tv_juli, tv_pingfennum;
        private RatingBar rb_pingfen;
        private RelativeLayout business_rl;
        private LinearLayout project_ll;

        private ProductItemHolder(Context context) {
            super(context);
        }

        @Override
        protected View initView() {
            return View.inflate(UIUtils.getContext(), R.layout.seach_item, null);
        }

        @Override
        protected void assignView(View rootView) {
            //隐藏商家
            project_ll = (LinearLayout) rootView.findViewById(R.id.project_ll_all);
            business_rl = (RelativeLayout) rootView.findViewById(R.id.business_rl);
            iv_picture = (ImageView) rootView.findViewById(R.id.project_iv_icon);
            tv_name = (TextView) rootView.findViewById(R.id.project_tv_name);
            tv_price = (TextView) rootView.findViewById(R.id.project_tv_price);
            tv_zhekounum = (TextView) rootView.findViewById(R.id.project_tv_zhekounum);
            tv_juli = (TextView) rootView.findViewById(R.id.project_tv_juli);
            tv_pingfennum = (TextView) rootView.findViewById(R.id.project_tv_pingfennum);
            rb_pingfen = (RatingBar) rootView.findViewById(R.id.project_rb_pingfen);
            business_rl.setVisibility(View.GONE);
        }

        @Override
        protected void refreshView(SearchResultBean data) {
            if (data.getType() == 2) {
                if (StringUtils.isEmpty(data.getCouMerImage())) {
                    iv_picture.setImageResource(R.mipmap.morenpic);
                } else {
                    ImageLoaderHelper.displayImage(data.getCouMerImage(), iv_picture);
                }
                tv_name.setText(data.getCouMerName());
                tv_price.setText(MoneyShowTool.nolastDFStr(data.getSalePrice()) + "");
                tv_zhekounum.setText(data.getDiscount() + "% Off");
                if (new GMapUtil().getCurrentLat().equals("") || StringUtils.isEmpty(new GMapUtil().getCurrentLat())
                        || new
                        GMapUtil().getCurrentLng().equals("") || StringUtils.isEmpty(new GMapUtil().getCurrentLng())
                        || new GMapUtil().getCurrentLng().equals("0.0") || new GMapUtil().getCurrentLat().equals("0" +
                        ".0")) {
                    tv_juli.setText(R.string.common_unknown);
                } else {
                    try {
                        if (data.getDistance() > 0) {
                            tv_juli.setText(DistanceUtils.formatMeter(data.getDistance()));
                        } else {
                            tv_juli.setText(R.string.common_unknown);
                        }
                    } catch (Throwable t) {
                        tv_juli.setText(R.string.common_unknown);
                    }
                }
                //tv_juli.setText(data.getDistance() + "");
                try {
                    rb_pingfen.setRating(Float.parseFloat(data.getEvaluateScore()));
                    tv_pingfennum.setText(Float.parseFloat(data.getEvaluateScore()) + "");
                } catch (Throwable t) {
                    rb_pingfen.setRating(5f);
                    tv_pingfennum.setText("5.0");
                }
            } else {
                project_ll.setVisibility(View.GONE);
            }
        }
    }

    class CommentItemHolder extends BaseViewHolder<SearchResultBean> {
        private ImageView iv_picture;
        private TextView tv_name, tv_price, tv_zhekounum, tv_juli, tv_pingfennum;
        private RatingBar rb_pingfen;
        private RelativeLayout business_rl;
        private ImageView business_iv_icon;
        private TextView business_tv_name, business_tv_address, business_tv_juli, business_tv_pingfennum;
        private RatingBar business_rb_pingfen;
        private LinearLayout project_ll;

        private CommentItemHolder(Context context) {
            super(context);
        }

        @Override
        protected View initView() {
            return View.inflate(UIUtils.getContext(), R.layout.seach_item, null);
        }

        @Override
        protected void assignView(View rootView) {
            business_rl = (RelativeLayout) rootView.findViewById(R.id.business_rl);
            project_ll = (LinearLayout) rootView.findViewById(R.id.project_ll_all);
            iv_picture = (ImageView) rootView.findViewById(R.id.project_iv_icon);
            tv_name = (TextView) rootView.findViewById(R.id.project_tv_name);
            tv_price = (TextView) rootView.findViewById(R.id.project_tv_price);
            tv_zhekounum = (TextView) rootView.findViewById(R.id.project_tv_zhekounum);
            tv_juli = (TextView) rootView.findViewById(R.id.project_tv_juli);
            tv_pingfennum = (TextView) rootView.findViewById(R.id.project_tv_pingfennum);
            rb_pingfen = (RatingBar) rootView.findViewById(R.id.project_rb_pingfen);

            business_iv_icon = (ImageView) rootView.findViewById(R.id.business_iv_icon);
            business_tv_name = (TextView) rootView.findViewById(R.id.business_tv_name);
            business_tv_address = (TextView) rootView.findViewById(R.id.business_tv_address);
            business_tv_juli = (TextView) rootView.findViewById(R.id.business_tv_juli);
            business_tv_pingfennum = (TextView) rootView.findViewById(R.id.business_tv_pingfennum);
            business_rb_pingfen = (RatingBar) rootView.findViewById(R.id.business_rb_pingfen);
        }

        @Override
        protected void refreshView(SearchResultBean data) {
            //根据 type隐藏或显示2商品或1商家
            if (data.getType() == 2) {
                business_rl.setVisibility(View.GONE);
                project_ll.setVisibility(View.VISIBLE);
                if (StringUtils.isEmpty(data.getCouMerImage())) {
                    iv_picture.setImageResource(R.mipmap.morenpic);
                } else {
                    ImageLoaderHelper.displayImage(data.getCouMerImage(), iv_picture);
                }
                tv_name.setText(data.getCouMerName());
                tv_price.setText(MoneyShowTool.nolastDFStr(data.getSalePrice()) + "");
                tv_zhekounum.setText(MoneyShowTool.nolastDFStr(data.getDiscount()) +  "% Off");
                if (new GMapUtil().getCurrentLat().equals("") || StringUtils.isEmpty(new GMapUtil().getCurrentLat())
                        || new
                        GMapUtil().getCurrentLng().equals("") || StringUtils.isEmpty(new GMapUtil().getCurrentLng())
                        || new GMapUtil().getCurrentLng().equals("0.0") || new GMapUtil().getCurrentLat().equals("0" +
                        ".0")) {
                    tv_juli.setText(R.string.common_unknown);
                } else {
                    try {
                        if (data.getDistance() > 0) {
                            tv_juli.setText(DistanceUtils.formatMeter(data.getDistance()));
                        } else {
                            tv_juli.setText(R.string.common_unknown);
                        }
                    } catch (Throwable t) {
                        tv_juli.setText(R.string.common_unknown);
                    }
                }
                if (Float.parseFloat(MoneyShowTool.formatOneStr(data.getEvaluateScore())) > 0) {
                    try {
                        rb_pingfen.setRating(Float.parseFloat(MoneyShowTool.formatOneStr(data.getEvaluateScore())));
                        tv_pingfennum.setText(Float.parseFloat(MoneyShowTool.formatOneStr(data.getEvaluateScore())) +
                                "");
                    } catch (Throwable t) {
                        rb_pingfen.setRating(5f);
                        tv_pingfennum.setText("5.0");
                    }
                } else {
                    rb_pingfen.setRating(5f);
                    tv_pingfennum.setText("5.0");
                }
            } else {
                business_rl.setVisibility(View.VISIBLE);
                project_ll.setVisibility(View.GONE);
                if (StringUtils.isEmpty(data.getCouMerImage())) {
                    business_iv_icon.setImageResource(R.mipmap.morenpic);
                } else {
                    ImageLoaderHelper.displayImage(data.getCouMerImage(), business_iv_icon);
                }
                business_tv_name.setText(data.getCouMerName());
                business_tv_address.setText(data.getMerchantAddress() + "");
//                business_tv_juli.setText(data.getDiscount());
                if (new GMapUtil().getCurrentLat().equals("") || StringUtils.isEmpty(new GMapUtil().getCurrentLat())
                        || new
                        GMapUtil().getCurrentLng().equals("") || StringUtils.isEmpty(new GMapUtil().getCurrentLng())
                        || new GMapUtil().getCurrentLng().equals("0.0") || new GMapUtil().getCurrentLat().equals("0" +
                        ".0")) {
                    tv_juli.setText(R.string.common_unknown);
                } else {
                    try {
                        if (!StringUtils.isEmpty(data.getDistance() + "")) {
                            tv_juli.setText(DistanceUtils.formatMeter(data.getDistance()));
                        } else {
                            tv_juli.setText(R.string.common_unknown);
                        }
                    } catch (Throwable t) {
                        tv_juli.setText(R.string.common_unknown);
                    }
                }
                if (Float.parseFloat(MoneyShowTool.formatOneStr(data.getEvaluateScore())) > 0) {
                    try {
                        business_rb_pingfen.setRating(Float.parseFloat(MoneyShowTool.formatOneStr(data
                                        .getEvaluateScore()

                        )));
                        business_tv_pingfennum.setText(Float.parseFloat(MoneyShowTool.formatOneStr(data.getEvaluateScore
                                ())) + "");
                    } catch (Throwable t) {
                        business_rb_pingfen.setRating(5f);
                        business_tv_pingfennum.setText("5.0");
                    }
                } else {
                    business_rb_pingfen.setRating(5f);
                    business_tv_pingfennum.setText("5.0");
                }
            }
        }
    }
}
